package cn.tuning.service.impl;

import cn.tuning.dao.OrderDao;
import cn.tuning.dao.UserDao;
import cn.tuning.entity.OrderInfo;
import cn.tuning.entity.RiderInfo;
import cn.tuning.entity.UserInfo;
import cn.tuning.pojo.APIResult;
import cn.tuning.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author lixiaolong
 * @date 2021-08-06 21:59
 * @description:
 */

@Service
public class OrderServiceImpl  implements OrderService {
    @Autowired
    OrderDao orderDao;
    @Autowired
    UserDao userDao;

    @Override
    public APIResult customerQueryOrder(String customerId) {
        APIResult apiResult = null;
        List<OrderInfo> orderInfoList = this.orderDao.listOrdersByCustomerId(customerId);
        System.out.println(orderInfoList);
        apiResult = APIResult.success(orderInfoList);
        return apiResult;
    }

    @Override
    public APIResult riderQueryOrder(String riderId) {
        APIResult apiResult = null;
        List<OrderInfo> orderInfoList = this.orderDao.listOrdersByRiderId(riderId);
        apiResult = APIResult.success(orderInfoList);
        return apiResult;
    }

    @Override
    public APIResult publishOrder(OrderInfo orderInfo) {
        this.orderDao.insertOrder(orderInfo);//
        this.userDao.updateUserByBalanceCustomer(orderInfo.getCustomerId(),orderInfo.getCommission());//用户表支出
        this.userDao.updateCustomerBalance(orderInfo.getCustomerId(),orderInfo.getCommission());
        APIResult apiResult = APIResult.success();
        return apiResult;
    }

    @Override
    public APIResult finishOrder(String riderId, String orderId) {
        APIResult apiResult =null;
        Date finishTime = new Date(); //获得时间,插入完成时间
        this.orderDao.updateOrderByIdOfFinish(orderId,2,finishTime);//跟新订单表
        OrderInfo orderInfo = this.orderDao.getOrderByOrderId(orderId);//通过orderId得到订单表
        int commission = orderInfo.getCommission();//获取佣金
        RiderInfo riderInfo = this.userDao.getRiderInfoById(riderId);
        riderInfo.setIncome(riderInfo.getIncome()+commission);//佣金给骑手
        riderInfo.setCurrentOrdersFinished(riderInfo.getCurrentOrdersFinished()+1);//完成单数+1
        riderInfo.setCurrentOrdersReceived(riderInfo.getCurrentOrdersReceived()-1);//当前接单数-1
        this.userDao.updateRiderInfo(riderInfo);//更改骑手表
        UserInfo userInfo = new UserInfo();
        this.userDao.updateUserByBalance(riderId,commission);//跟新用户表
        apiResult = APIResult.success();
        return apiResult;
    }

    @Override
    public APIResult takeOrder(String riderId, String orderId) {
        APIResult apiResult = null;
        //获取现在时间
        Date receivedTime = new Date();
        this.orderDao.updateOrderOfByIdReceive(riderId,1,receivedTime,orderId);//更改订单表
        this.userDao.updateRiderByCurrentReceive(riderId);//更改骑手表
        return  APIResult.success();
    }

    @Override
    public APIResult riderRegister(RiderInfo riderInfo) {
        this.userDao.insertRiderInfo(riderInfo);
        return  APIResult.success();
    }



    @Override
    public APIResult riderReceive(String riderId) {
        APIResult apiResult = null;
        //判断是否已经注册
        RiderInfo riderInfo = this.userDao.getRiderInfoById(riderId);
        if (Objects.isNull(riderInfo)){
            return APIResult.riderISNotFind();
        }
        return APIResult.success(this.orderDao.listOrdersByOrderStatus(0));
    }


    @Override
    public APIResult getUserMassage(String username) {
        return  APIResult.success(this.userDao.getUserByUsername(username)) ;
    }

    @Override
    public APIResult getRiderMassage(String riderId) {
        return APIResult.success(this.userDao.getRiderInfoById(riderId));
    }
}
